package tw.miku.protocol.cus;

import java.nio.charset.CharacterCodingException;

import org.apache.mina.core.session.IoSession;

import tw.miku.GameMain;
import tw.miku.model.Group;
import tw.miku.model.Room;
import tw.miku.model.User;
import tw.miku.model.gold.GoldGame;
import tw.miku.protocol.codec.AbstractRequestMessagePack;
import tw.miku.protocol.codec.gold.GoldCompleteRequestPack;
import tw.miku.protocol.codec.gold.GoldCompleteResponsePack;

public class GoldCompleteHandler implements IMessageHandler
{

	public GoldCompleteHandler()
	{
		
	}

	@Override
	public void handleData(AbstractRequestMessagePack message, IoSession session)
			throws CharacterCodingException
	{
		GoldCompleteRequestPack pack = (GoldCompleteRequestPack) message;
		if (null != pack)
		{
			Group group = GameMain.getGroup();
			User user = group.getUser(session.getId());
			Room room = user.getRoom();
			if (null != user && null != room)
			{
				GoldGame game = (GoldGame) room.getGame();
				if (game.isGameStart())
				{ 
					GoldCompleteResponsePack result = new GoldCompleteResponsePack();
					result.userId = user.getId();
					result.direct = pack.result;
					if(game.reached())
					{
						result.result = true;
						room.send(result.encode());
					}
					else
					{
						result.result = false;
						user.send(result.encode());
					}
				}
				
			}
		}
	}

}
